package com.xm.security.config.security;

import com.xm.security.config.properties.WebCorsProperties;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * 跨域处理
 *
 * @author yousj
 * @since 2021/8/15
 */
@Configuration
@RequiredArgsConstructor
@ConditionalOnClass(SecurityConfig.class)
@EnableConfigurationProperties(WebCorsProperties.class)
public class WebCorsConfig extends WebMvcConfigurerAdapter {

    private final WebCorsProperties webCorsProperties;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping(webCorsProperties.getAddMapping())
                .allowedOrigins(webCorsProperties.getAllowedOrigins())
                .allowedMethods(webCorsProperties.getAllowedMethods())
                .allowCredentials(webCorsProperties.getAllowCredentials())
                .maxAge(webCorsProperties.getMaxAge());
    }



}
